yjx.controller('cRelationshipController',['$rootScope','$scope','$stateParams','$fetch','modalService','ipCookie','$timeout',function ($rootScope,$scope,$stateParams,$fetch,modalService,ipCookie,$timeout) {
    $scope.customerId = $stateParams.customerId;
    $scope.name = $stateParams.name;
    /*获取该客户的关系列表*/
    function getRelationList(){
        $fetch.fetch(
            '/customer/navigation/data',
            {
                params:{
                    customerId:$scope.$parent.customerId,
                    dataType:'t'
                },
                type:'get',
                cache:false
            }
        ).then(function(data){
                $timeout(function(){
                    // console.log( data)
                    if(data){
                        $scope.share.relationList = data;
                        $scope.$parent.resetHeight();                       ;
                    }
                })
            })
    }
    if($scope.$parent.customerId) {
        // $scope.share.relationList = [];
        getRelationList();
    }
    /**/


    /*预约客户*/
    var reservationMsg = "该用户已预约成功！<br>您将在待办事项中收到提醒！"
    $scope.reservation = function (id) {
        /*调接口*/
        $fetch.fetch(
            '/customer/bespeak',
            {
                params:{
                    userId:$rootScope.userId,
                    customerIds:id,
                    type:1
                },
                cache:false
            }
        ).then(function(data){
                modalService.mainMsgInfo("待办事项中将出现这些提醒",'','预约成功')
            }).catch(function(e){
                modalService.mainMsgError(e.msg);
            })
    }
    /**删除关系 */
    $scope.deleteRelation = function (id) {
        $scope.deleteId = id;
        modalService.confirm("是否删除该用户关系",confirm)
    }
    function confirm() {
        $fetch.fetch(
            '/customer/relation/del',
            {
                params:{
                    userId:$rootScope.userId,
                    customerRelationId:$scope.deleteId,
                },
                type:'POST'
            }).then(function(data){
                $timeout(function(){
                    // console.log($scope.deleteId)
                    if( $scope.deleteId ) {
                        for(var i in $scope.share.relationList ) {
                            if($scope.deleteId  == $scope.share.relationList[i].customerRelationId) {
                                // console.log($scope.share.relationList[i]);
                                if( $scope.share.clientList &&  $scope.share.clientList.length>0) {
                                    // $scope.share.clientList 可添加关系的客户
                                    $scope.share.clientList.push($scope.share.relationList[i]);
                                }else{
                                    $scope.share.clientList = new Array($scope.share.relationList[i]);
                                }
                                $scope.share.relationList.splice(i,1);
                                $scope.$parent.resetHeight();
                                return;
                            }
                        }
                    }
                })
                }).catch(function(e){
                modalService.mainMsgError(e.msg)
            })
    }

}])